//import { gp } from '@vui'
import isEmpty from 'lodash/isEmpty'
import trim from 'lodash/trim'
import isPlainObject from 'lodash/isPlainObject'
import qs from 'qs'
//import { translateTitle } from '@/utils/i18n'
import { getStorage, getUrlSystemApp, encryptd } from '@/utils/tools'
import { GFCD } from '@/utils/utils'

//必须带done回调 执行完成调用
export default function openPrintLink({ vm, action, callbackData, done }) {
  console.log('callbackData-1---', callbackData)
  // if (!action.extension.url.value) {
  //   done(null, {})
  //   return
  // }

  // if (vm.$store.getters['preview/getDev']) {
  //   gp.$baseMessage(translateTitle('Ve.eventTask.预览阶段不执行跳转链接'), 'warning')
  //   done(null, {})
  //   return
  // }
  let _data = {}
  if (!isEmpty(callbackData)) {
    // if (vm.info.type == 'uiTable') {
    //   _data = callbackData[0]
    // } else {
    //   _data = callbackData.data[Object.keys(callbackData.data)[0]]
    // }
    // if (Array.isArray(callbackData)) {
    //   // 如果回调值为数组 则取数组的第一项
    //   _data = callbackData[0]
    // } else
    if (isPlainObject(callbackData.data)) {
      // 如果有 data 值且为对象的话 则认为是接口返回值，则取data下的值
      _data = callbackData.data[Object.keys(callbackData.data)[0]]
    } else {
      _data = callbackData
    }

    // _data = { SKU: '123', TEST: 12 }
  }

  let param = {}
  if (action.extension.param) {
    let _arr = action.extension.param.value.split(',')
    _arr.forEach((v) => {
      if (!isEmpty(v)) {
        let _key = trim(v)
        if (Array.isArray(_data)) {
          // 如果是数组则需要进行值拼接
          const valList = []
          _data.forEach((i) => {
            valList.push(i[_key])
          })
          param[_key] = valList.join()
        } else {
          param[_key] = _data[_key]
        }
      }
    })
  }

  //增加token传参
  param['TOKEN'] = getStorage(`token_${getUrlSystemApp()}`)

  //ase加密传参
  param = encryptd(qs.stringify(param))

  let _url = ``
  //let url2: `${GFCD('dams').domain}/dams/dams-web`,
  if (isEmpty(param)) {
    //_url = `${action.extension.url.value}`
    _url = `${GFCD('wms').domain}/ve_print/`
  } else {
    //_url = `${action.extension.url.value}?${param}`
    _url = `${GFCD('wms').domain}/ve_print/?${param}`
  }

  console.log('openLink:', _url.slice(1))
  if (action.extension.hasNewOpenWindow.value) {
    window.open(_url)
  } else {
    // location.href = _url
    vm.$router.push(_url.slice(1))
  }
  setTimeout(() => {
    done(null, {})
  }, 1000)
}
